package CalendarComponents;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;

import calendar.GlobalSettings;

import Managers.Tone;

@SuppressWarnings("serial")
public class BusyMeter extends JPanel
{
	private double busyness;
	private int width;
	private double old_val;
	
	//COLOR BLIND MODE SWITCHES FROM USING A RED GREEN COMBINATION TO A BLUE MAGENTA COMBINATION
	//THIS TAKES INTO ACCOUNT RED/GREEN AND BLUE/YELLOW COLOR BLINDESS
	//ACORDING TO THE DATA ON THIS SITE
	//http://www.stcsig.org/usability/newsletter/9910-color-blindness.html
	//WE SHOULND'T RELY ON A COLOR BAR ALONE ANOTHER GOOD REASON TO USE THE TONE CLASS I ADAPTED :)
	
	//constructor
	public BusyMeter(int width, int height)
	{
		busyness = 0.0;
		old_val = busyness;
		this.width = width;
		this.setPreferredSize(new Dimension(width, height));
	}
	
	
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		
		Graphics2D g2d = (Graphics2D)g;
		
		g2d.setColor(GlobalSettings.color_blind_mode ? Color.MAGENTA : Color.GREEN);
			 
		//draw Green Box
		g2d.fillRect(0, 0, width, (int)(getHeight()*(1.0 - busyness)));
		
		//Draw Red Box
		g2d.setColor(GlobalSettings.color_blind_mode ? Color.BLUE : Color.RED);
		g2d.fillRect(0, (int)(getHeight()*(1.0 - busyness)), width, (int)(getHeight()*busyness));
		
		//draw Seperator box
		g2d.setColor(Color.BLACK);
		g2d.fillRect(0, (int)((getHeight()*(1.0 - busyness))-2), width, 3);
	}

	public void setMeter(int n)
	{
		//if it's over twenty then it's at the maximum
		if(n > 20)
		{
			old_val = busyness;
			
			busyness = 1.0;
			
			if(old_val != busyness)
			{
				Tone.playTone(20);	
			}
			
			this.repaint();//update the screen
		}
		else
		{
			old_val = busyness;
			
			busyness = (double)n / 20.0;//get a busyness factor between 0 and 1.0

			if(old_val != busyness)
			{
				Tone.playTone(n);	
			}
			
			this.repaint();//update the screen
		}
	}
}
